#coding=utf-8

#import guidata
import numpy as np
from guiqwt.pyplot import *
from guiqwt.curve import *
from guiqwt.builder import make
from guiqwt.plot import CurveDialog
#from guiqwt.plot import PlotManager
#from PyQt5.QtWidgets import QDialog

class CPlotManager():
    
    def __init__(self, cdata, dlgParent = None):

        #self.plot = CurvePlot(title = 'Example')
        self.win = CurveDialog(edit = False,  toolbar = True, wintitle = 'Scope', parent = dlgParent, 
                                options = dict(title = 'Scope', xlabel = 'time/sample',  ylabel = 'value'))
        
        #self.plots = []
        self.curves = [make.curve([0], [0], color = 'blue'), 
                       make.curve([0], [0], color = 'red'), 
                       make.curve([0], [0], color = 'yellow'), 
                       make.curve([0], [0], color = 'green')]
        self.plot = self.win.get_plot()
        
        for curve in self.curves:
            self.plot.add_item(curve)
            
        self.plot.add_item(make.legend('TR'))
        #self.plot.add_item(make.itemList())
            
        self.cdata = cdata
        
        self.ymin = 0
        self.ymax = 0
        
        self.timeSpan = 10 * 8000
        
    def GetWin(self):
        return self.win


    def ReInit(self):
        
        index = 0
        for curve in self.curves:
            curve.set_data([0], [0])
            curve.setTitle(self.cdata.dataList[index].name)
            index = index + 1
            
        self.plot.set_axis_limits('bottom', 0, self.timeSpan)
        
        for curve in self.curves:
            try:
                self.plot.del_item(curve)
            except:
                continue
        
        
        for ch in range(0, len(self.cdata.dataList)):
            if self.cdata.dataList[ch].addr != 0:
                self.plot.add_item(self.curves[ch])
        
        
    def Show(self):        
        self.win.show()

        
    def Update(self):
       
        for ch in range(0, len(self.cdata.dataList)): 
            
            if self.cdata.dataList[ch].addr == 0 or ch > len(self.curves):
                continue
                
            length = self.cdata.dataList[ch].length()
            if length < self.timeSpan:
                t = np.linspace(0, length - 1, length)
                self.curves[ch].set_data(t, self.cdata.dataList[ch].data[0:length])
                #self.GetRange(self.cdata.dataList[ch].data,  0,  length)
            else:
                t = np.linspace(0, self.timeSpan - 1, self.timeSpan)
                self.curves[ch].set_data(t, self.cdata.dataList[ch].data[length - self.timeSpan:length])
                #self.GetRange(self.cdata.dataList[ch].data,  length - self.timeSpan,  length)

            

        #self.plot.set_axis_limits('left',  self.ymin,  self.ymax)
        #self.plot.replot()
        self.plot.do_autoscale()
        
    def GetRange(self,  data,  xmin,  xmax):
        
        for datum in data[xmin:xmax]:
            if datum > self.ymax:
                self.ymax  = datum
            if datum < self.ymin:
                self.ymin = datum
        

    """ Finalize the disaplay, put all data to the curve"""
    def Finalize(self):
        
        for ch in range(0, len(self.cdata.dataList)): 
            
            if self.cdata.dataList[ch].addr == 0 or ch > len(self.curves):
                continue
                
            length = self.cdata.dataList[ch].length()
            t = np.linspace(0, length - 1, length)
            self.curves[ch].set_data(t, self.cdata.dataList[ch].data[0:length])

        self.plot.do_autoscale()        
